/**
 * 模板引擎第八版
 */
 var _ = {};

 _.templateSettings = {
     // 求值
     evaluate: /<%([\s\S]+?)%>/g,
     // 插入
     interpolate: /<%=([\s\S]+?)%>/g,
     // 转义
     escape: /<%-([\s\S]+?)%>/g
 };
 
 var noMatch = /(.)^/;
 
 var escapes = {
     "'": "'",
     '\\': '\\',
     '\r': 'r',
     '\n': 'n',
     '\u2028': 'u2028',
     '\u2029': 'u2029'
 };
 
 var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g;
 
 var escapeChar = function(match) {
     return '\\' + escapes[match];
 };
 
 _.template = function(text, settings) {
 
     settings = Object.assign({}, _.templateSettings, settings);
 
     var matcher = RegExp([
         (settings.escape || noMatch).source,
         (settings.interpolate || noMatch).source,
         (settings.evaluate || noMatch).source
     ].join('|') + '|$', 'g');
 
     var index = 0;
     var source = "__p+='";
     text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
 
         source += text.slice(index, offset).replace(escapeRegExp, escapeChar);
 
         index = offset + match.length;
 
         if (escape) {
             source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
         } else if (interpolate) {
             source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
         } else if (evaluate) {
             source += "';\n" + evaluate + "\n__p+='";
         }
 
         return match;
     });
     source += "';\n";
 
     if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
 
     source = "var __t,__p='',__j=Array.prototype.join," +
         "print=function(){__p+=__j.call(arguments,'');};\n" +
         source + 'return __p;\n';
 
     var render;
     try {
         render = new Function(settings.variable || 'obj', '_', source);
     } catch (e) {
         e.source = source;
         throw e;
     }
 
     var template = function(data) {
         return render.call(this, data, _);
     };
 
     var argument = settings.variable || 'obj';
     template.source = 'function(' + argument + '){\n' + source + '}';
 
     return template;
 };
 
 var results = document.getElementById("container");
 
 var data = {
     users: [
         { "name": "Byron", "url": "http://localhost" },
         { "name": "Casper", "url": "http://localhost" },
         { "name": "Frank", "url": "http://localhost" }
     ]
 }
 
 var text = document.getElementById("user_tmpl").innerHTML
 var compiled = _.template(text);
 
//  console.log(compiled.source)
 results.innerHTML = compiled(data);